/*
d * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.lucene.analysis.ik;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.analysis.util.ResourceLoader;
import org.apache.lucene.analysis.util.ResourceLoaderAware;
import org.apache.lucene.analysis.util.TokenizerFactory;
import org.apache.lucene.util.AttributeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wltea.analyzer.cfg.SolrIKConfig;
import org.wltea.analyzer.dic.Dictionary;
import org.wltea.analyzer.lucene.IKTokenizer;

/**
 * 
 * @author piaohailin
 * @date 2016年7月13日
 * 
 *       在schema.xml配置如下代码</br>
 *       ext.dic和stopword.dic必须为utf8格式</br>
 *       参考文档</br>
 *       http://grunt1223.iteye.com/blog/969197</br>
 * 
 */
// <fieldType name="text_ik" class="solr.TextField" positionIncrementGap="100">
// <analyzer type="index">
// <tokenizer class="org.apache.lucene.analysis.ik.IKTokenizerFactory"
// name="hellosolr"/>
// <filter class="solr.LowerCaseFilterFactory"/>
// </analyzer>
// <analyzer type="query">
// <tokenizer class="org.apache.lucene.analysis.ik.IKTokenizerFactory"
// name="hellosolr"/>
// <filter class="solr.LowerCaseFilterFactory"/>
// </analyzer>
// </fieldType>

public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoaderAware {
	private Logger logger = LoggerFactory.getLogger(IKTokenizerFactory.class);

	private boolean ignoreCase;
	private String extStopwords;
	private String extDict;
	private SolrIKConfig config = new SolrIKConfig();

	public IKTokenizerFactory(Map<String, String> args) {
		super(args);

		this.ignoreCase = getBoolean(args, "ignoreCase", true);
		this.config.setIgnoreCase(ignoreCase);
		logger.info("ignoreCase = " + this.extStopwords);

		this.extStopwords = get(args, SolrIKConfig.EXT_STOPWORDS);
		this.config.setExtStopwords(extStopwords);
		logger.info("EXT_STOPWORDS = " + this.extStopwords);

		this.extDict = get(args, SolrIKConfig.EXT_DICT);
		this.config.setExtDict(extDict);
		logger.info("EXT_DICT = " + this.extDict);

		if (!args.isEmpty()) {
			throw new IllegalArgumentException("Unknown parameters: " + args);
		}
	}

	@Override
	public IKTokenizer create(AttributeFactory factory) {
		IKTokenizer tokenizer = new IKTokenizer(config);
		return tokenizer;
	}

	@Override
	public void inform(ResourceLoader loader) throws IOException {
		this.config.setLoader(loader);
		this.config.getDictionary().init();

		Collection<String> words = new ArrayList<String>();
		// 扩展字典
		if (extDict != null) {
			CharArraySet dicts = getWordSet(loader, this.extDict, ignoreCase);
			for (Object dic : dicts) {
				logger.info("扩展字典：" + new String((char[]) dic));
				words.add(new String((char[]) dic));
			}
			config.getDictionary().addWords(words);
			words.clear();
		}
		
		// 扩展停止词
		if (extStopwords != null) {
			CharArraySet stopWords = getWordSet(loader, this.extStopwords, ignoreCase);
			for (Object dic : stopWords) {
				logger.info("扩展停止词：" + new String((char[]) dic));
				words.add(new String((char[]) dic));
			}
			config.getDictionary().addStopWords(words);
			words.clear();
		}
	}

}
